package com.cy.security;

import com.alibaba.fastjson.JSONObject;
import com.cy.config.EnumConst;

public class CommonUtil {

	public static Integer str2Int(String value, int base) {
		Integer result = Integer.parseInt(value, base);
		return result;
	}

	public static String int2Str(Integer value, int base, int length,
			String padding) {
		String result = "";

		base = (base <= 0) ? 10 : base;
		length = (length <= 0) ? 10 : length;
		padding = (String) ((padding.isEmpty() || padding.length() <= 0) ? "0"
				: "" + padding.charAt(0));

		switch (base) {
		case 2:
			result = Integer.toBinaryString(value);
			break;
		case 8:
			result = Integer.toOctalString(value);
			break;
		case 10:
			result = Integer.toString(value);
			break;
		case 16:
			result = Integer.toHexString(value);
			break;
		default:
			result = Integer.toString(value);
			break;
		}

		int size = result.length();
		if (size > length) {
			result = result.substring(size - length, size);
		} else {
			// left padding
			for (int i = 0; i < length - size; i++) {
				result = padding + result;
			}
		}

		return result;
	}

	public static String int2Str(long value, int base, int length,
			String padding) {
		String result = "";

		base = (base <= 0) ? 10 : base;
		length = (length <= 0) ? 10 : length;
		padding = (String) ((padding.isEmpty() || padding.length() <= 0) ? "0"
				: "" + padding.charAt(0));

		switch (base) {
		case 2:
			result = Long.toBinaryString(value);
			break;
		case 8:
			result = Long.toOctalString(value);
			break;
		case 10:
			result = Long.toString(value);
			break;
		case 16:
			result = Long.toHexString(value);
			break;
		default:
			result = Long.toString(value);
			break;
		}

		int size = result.length();
		if (size > length) {
			result = result.substring(size - length, size);
		} else {
			// left padding
			for (int i = 0; i < length - size; i++) {
				result = padding + result;
			}
		}

		return result;
	}

	public static String ltrim(String str, String padding) {
		str = str.trim();
		String res = "";
		padding = (padding.isEmpty() || padding.length() == 0) ? "0" : ""
				+ padding.charAt(0);

		int size = str.length();
		boolean bFound = false;
		int i = 0;
		for (i = 0; i < size && !bFound; i++) {
			bFound = (padding.compareToIgnoreCase("" + str.charAt(i)) == 0);
		}

		if (bFound) {
			res = str.substring(i + 1, size);
		} else {
			res = "0";
		}
		return res;
	}
	
	public static JSONObject parseRetResult(String result) {
		result = result.replace("{", "");
		result = result.replace("}", "");
		if (!result.contains("##")) {
			result = result.replace(",", "");
		}
		result = result.trim();
		String tn = result.substring(0, 2);
		JSONObject jsonObject = new JSONObject();

		jsonObject.put("tn", tn);
		String rt = result.substring(2, 4);
		jsonObject.put("rt", rt);

		/**
		 * 66 锅炉监测控制 67 锅炉D参数设置 68 锅炉T参数设置
		 */

		if (EnumConst.Option.SETPOWER.getCode().equals(tn)) {
			if ("01".equals(rt) && result.length() >4) {
					jsonObject.put("pl", result.substring(4, 6));
//				if(result.length() > 27){
					jsonObject.put("tc", result.substring(6, 10));
					jsonObject.put("ds", result.substring(10, 14));
					jsonObject.put("t", result.substring(14, 18));
//				66010017BB02B0003C00  {66410F3929114005DCD9DD}
					if(result.length()>18){
						jsonObject.put("fv", result.substring(18, 20));
						System.out.println("FV=" + result.substring(18, 20));
					}
					if(result.length()>20){

						jsonObject.put("fs", result.substring(20, 24));
						System.out.println("Fs=" + result.substring(20, 24));
					}
					if(result.length()>27){

						jsonObject.put("te", result.substring(24, 28));
					}
					if(result.length()>31){

						jsonObject.put("de", result.substring(28, 32));
					}
//				}else{   6601 3F 0066 0011 52 0A32 00 D2 C7
//					jsonObject.put("pl", result.substring(4, 6));
					jsonObject.put("td", result.substring(6, 10));
					jsonObject.put("te", result.substring(10, 14));
					jsonObject.put("fr", result.substring(14, 16));
					jsonObject.put("fsN", result.substring(16, 20));
					jsonObject.put("v5", result.substring(20, 22));
					jsonObject.put("ss", result.substring(22, 24));
//				}

			}
		}else if (EnumConst.Option.AABREW.getCode().equals(tn)) {
			if ("01".equals(rt) && result.length() > 6) {
				jsonObject.put("ss", result.substring(4, 6));
			}
		}

		else if (EnumConst.Option.GETREGCODE.getCode().equals(tn)) {
			if ("01".equals(rt)) {
				if(result.length() > 35){
					jsonObject.put("dn", result.substring(4, 28));
					jsonObject.put("hv", result.substring(28, 32));
					jsonObject.put("fv", result.substring(32, 36));
				}else{
					jsonObject.put("dn", result.substring(4, 18));
					jsonObject.put("hv", result.substring(18, 22));
					jsonObject.put("fv", result.substring(22, 26));
				}

			}
		}
		else if (EnumConst.Option.GETBREWRECORD.getCode().equals(tn)) {//获取冲泡记录
			if ("01".equals(rt)) {
				String[] strings = result.split("##");
				if(strings.length > 1) {
					jsonObject.put("ds", strings[1].replace(",", ";"));
					result = result.replace(",", "");
					result = result.trim();
					jsonObject.put("bh", result.substring(4, 6));
					//因手机客户端bug，先临时切换成后台 解析成10进制
					jsonObject.put("sz", Integer.parseInt(result.substring(6, 8),16));
				}
			}
		}else if (EnumConst.Option.SAYHI.getCode().equals(tn) || EnumConst.Option.ECHORESPONSE.getCode().equals(tn)) {// Hello鉴权、Echo响应
			if ("01".equals(rt)) {// 如果成功
				jsonObject.put("ss", result.substring(4, 6));
				jsonObject.put("rc", result.substring(6, 10));
			}
		}else if (EnumConst.Option.SETSTATUSKEEPTIME.getCode().equals(tn)) {// 设置状态保持时间
			if ("01".equals(rt) && result.length() >5) {// 如果成功
				jsonObject.put("cr", result.substring(4, 5));
				jsonObject.put("br", result.substring(5, 6));
				jsonObject.put("pr", result.substring(6, 8));
			}
		}else if (EnumConst.Option.GETDEVICESTA.getCode().equals(tn)) {// 获取本机冲泡参数
			if ("01".equals(rt)) {
				jsonObject.put("bc", result.substring(4, 6));
				jsonObject.put("bt", result.substring(6, 8));
				jsonObject.put("sv", result.substring(8, 10));
				jsonObject.put("bv5", result.substring(10, 12));
				jsonObject.put("st", result.substring(12, 14));
				jsonObject.put("pt", result.substring(14, 16));
				jsonObject.put("ht", result.substring(16, 18));
			}
		}else if (EnumConst.Option.UPDATEFIRM.getCode().equals(tn)) {// 升级固件传输
			if ("01".equals(tn)) {
				jsonObject.put("fv", result.substring(4, 8));
				jsonObject.put("nd", result.substring(8, 12));
				jsonObject.put("sz", result.substring(12, 14));
				jsonObject.put("cs", result.substring(14, 16));
			}
		}else if (EnumConst.Option.WATERTEST.getCode().equals(tn)) {// 出水流速测试
			if ("01".equals(rt) && result.length() > 4) {
				jsonObject.put("fl", result.substring(4, 8));
				jsonObject.put("cn", result.substring(8, 12));
				jsonObject.put("mn", result.substring(12, 14));
				jsonObject.put("fs", result.substring(14, 18));
			}
		}else if (EnumConst.Option.SETRTVAL.getCode().equals(tn)) {// 光耦阈值设置
			if ("01".equals(rt)) {
				jsonObject.put("ot5", result.substring(4, 8));
				jsonObject.put("o5", result.substring(8, 12));
			}
		}else if (EnumConst.Option.SETCOEFFI.getCode().equals(tn)) {// 加热系数设定  {6901,0014}
			if ("01".equals(rt) && result.length() > 7) {
				jsonObject.put("pr", result.substring(4, 8));
			}
		}else if (EnumConst.Option.SETTEMPCOMPEN.getCode().equals(tn)) {// 温度补偿系数设定
			if ("01".equals(rt)) {
				jsonObject.put("gama", result.substring(4, 8));
			}
		}else if ( EnumConst.Option.FLOWSET.getCode().equals(tn)) {// 64 流量计参数设置
			if ("01".equals(rt) && result.length() > 6) {
				jsonObject.put("kv", result.substring(4, 8));
			}
		}else if ( EnumConst.Option.SETMODELPARAM.getCode().equals(tn)) {// 6B 动态温控模型参数  {6B01,4DAD0000DEA4}
			if ("01".equals(rt)) {
				jsonObject.put("a", result.substring(4, 8));
				jsonObject.put("b", result.substring(8, 12));
				jsonObject.put("c", result.substring(12, 16));
				if(result.length()>18){
					jsonObject.put("d", result.substring(16, 20));
				}
			}
		}else if ( EnumConst.Option.SETMODELXYZ.getCode().equals(tn)) {// 6C
			if ("01".equals(rt)) {
				jsonObject.put("x", result.substring(4, 8));
				jsonObject.put("y", result.substring(8, 12));
				jsonObject.put("z", result.substring(12, 16));

			}
		}else if ( EnumConst.Option.SETLINEAR.getCode().equals(tn)) {// 6D
			if ("01".equals(rt)) {
				jsonObject.put("f0", result.substring(4, 8));
				jsonObject.put("f1", result.substring(8, 12));
				jsonObject.put("f2", result.substring(12, 16));

			}
		}else if( EnumConst.Option.SETPARAM.getCode().equals(tn)
				|| EnumConst.Option.SETPARAMT.getCode().equals(tn) ){

			if ("01".equals(rt) && result.length() >4) {
				jsonObject.put("pl", result.substring(4, 6));
				jsonObject.put("tc", result.substring(6, 10));
				jsonObject.put("ds", result.substring(10, 14));
				jsonObject.put("t", result.substring(14, 18));
			}
		}

		/**
		 * 01 设置管理密码 02 设置状态保持时间 03 加解锁设定 04 设置离场锁定设定 05 设置设备有效工作时限 06 设置有效提醒日期
		 * 31 设置本机冲泡类型与温度 32 设置本机冲泡水量 33 设置本机冲泡时间 52 固件升级传输 54 固件升级确认
		 * 55 固件初始化 61 指示灯测试 62 音效测试
		 */
		/*else if (EnumConst.Option.SETOPTPASS.getCode().equals(tn) || EnumConst.Option.SETLOCKORUNLOCK.getCode().equals(tn)
				|| EnumConst.Option.SETLIVELOCK.getCode().equals(tn) || EnumConst.Option.SETVAILDTIME.getCode().equals(tn) 
				|| EnumConst.Option.SETREMINDTIME.getCode().equals(tn) || EnumConst.Option.WATERTEMPE.getCode().equals(tn)
				|| EnumConst.Option.WATERYIELD.getCode().equals(tn) || EnumConst.Option.SETBREWTIME.getCode().equals(tn) 
				|| EnumConst.Option.FIRMSURE.getCode().equals(tn) || EnumConst.Option.UPDATEFIRMSURE.getCode().equals(tn) 
				|| EnumConst.Option.INITDEVICE.getCode().equals(tn) || EnumConst.Option.LIGHTON.getCode().equals(tn) 
				|| EnumConst.Option.AUDIOON.getCode().equals(tn)
				|| EnumConst.Option.RESETPASSWORD.getCode().equals(tn) || EnumConst.Option.AAVOLUME.getCode().equals(tn)
				|| EnumConst.Option.AATIME.getCode().equals(tn) || EnumConst.Option.SETTIME.getCode().equals(tn)|| EnumConst.Option.RESETGK.getCode().equals(tn)) {
			jsonObject.put("tn", tn);
			jsonObject.put("rt", result.substring(2, 4));
		}*/

		return jsonObject;
	}
}
